package com.hisan.freeride.home.adapter;

import android.view.View;

import com.github.library.BaseQuickAdapter;
import com.github.library.BaseViewHolder;

import com.hisan.freeride.R;
import com.hisan.freeride.common.utils.CollectionUtils;
import com.hisan.freeride.common.utils.ConvertUtils;
import com.hisan.freeride.home.model.Release;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 *
 * 发布行程适配器
 * Created by yu on 2017/1/17.
 */

public class ReleaseAdapter extends BaseQuickAdapter<Release,BaseViewHolder> {
    private ButtonClickListener mButtonClickListener;
    private onItemClickListener mOnItemClickListener;

    public ReleaseAdapter(List<Release> data) {
        super(R.layout.home_release_item, data);
    }

    @Override
    protected void convert(BaseViewHolder holder, final Release data) {
        holder.setText(R.id.id,data.getId()+"");
        holder.getView(R.id.home).setOnClickListener(v -> {
            if (mOnItemClickListener != null){
                mOnItemClickListener.onItemClick(data);
            }
        });
        holder.setText(R.id.time,data.getDeparture_time());
//        holder.setText(R.id.total,data.getPeople()+"人");
        holder.setText(R.id.site,"出发地: "+data.getInitially());
        if (!CollectionUtils.isNullOrEmpty(data.getWay())){
            holder.setText(R.id.way,"途径地: "+data.getWay());
        }else {
            holder.setText(R.id.way,"途径地: ");
        }
        holder.setText(R.id.address,"目的地: "+data.getDestination());
        if (data.getReview()==0){
            holder.getView(R.id.cancel).setVisibility(View.GONE);
            holder.getView(R.id.cancel_name).setVisibility(View.VISIBLE);
            holder.setText(R.id.cancel_name,"待审核");
            holder.setText(R.id.look,"取消行程");
            holder.getView(R.id.look).setOnClickListener(null);
            holder.getView(R.id.look).setVisibility(View.VISIBLE);
            holder.getView(R.id.look).setOnClickListener(v -> {
                if (mButtonClickListener != null){
                    //取消发布的行程
                    mButtonClickListener.onClick(1,data);
                }
            });
        }else if (data.getReview()==1){

                holder.setText(R.id.cancel,"取消行程");
                holder.getView(R.id.cancel).setVisibility(View.VISIBLE);
                holder.setText(R.id.look,"查看预约");
                holder.getView(R.id.cancel_name).setVisibility(View.GONE);
                holder.getView(R.id.look).setVisibility(View.VISIBLE);
                holder.getView(R.id.cancel).setOnClickListener(v -> {
                    if (mButtonClickListener != null){
                        //取消发布的行程
                        mButtonClickListener.onClick(1,data);
                    }
                });
                holder.getView(R.id.look).setOnClickListener(v -> {
                    if (mButtonClickListener != null){
                        //查看预约的乘客
                        mButtonClickListener.onClick(2,data);
                    }
                });

        }else if (data.getReview()==2){
            holder.getView(R.id.cancel).setVisibility(View.GONE);
            holder.getView(R.id.cancel_name).setVisibility(View.VISIBLE);
            holder.setText(R.id.cancel_name,"行程被拒绝");
            holder.getView(R.id.look).setVisibility(View.GONE);
        }
        else if (data.getReview()==3){
            holder.getView(R.id.cancel).setVisibility(View.GONE);
            holder.getView(R.id.cancel_name).setVisibility(View.VISIBLE);
            holder.setText(R.id.cancel_name,"行程已取消");
            holder.getView(R.id.look).setVisibility(View.VISIBLE);
            holder.setText(R.id.look,"删除行程");
            holder.getView(R.id.look).setOnClickListener(v -> {
                if (mButtonClickListener != null){
                    //删除行程
                    mButtonClickListener.onClick(3,data);
                }
            });
        }
         String time=data.getDeparture_time()+":00";
          if (getTime(time)>0){
              holder.getView(R.id.cancel).setVisibility(View.GONE);
              holder.getView(R.id.cancel_name).setVisibility(View.VISIBLE);
              holder.setText(R.id.cancel_name,"行程已过期");
              holder.getView(R.id.look).setVisibility(View.VISIBLE);
              holder.setText(R.id.look,"删除行程");
              holder.getView(R.id.look).setOnClickListener(v -> {
                  if (mButtonClickListener != null){
                      //删除行程
                      mButtonClickListener.onClick(3,data);
                  }
              });
          }
    }



    public void setOnItemClickListener(onItemClickListener listener) {
        mOnItemClickListener = listener;
    }

    public void setButtonClickListener(ButtonClickListener listener) {
        mButtonClickListener = listener;
    }
    /**
     * 自定义的recyclerView点击监听
     */
    public interface onItemClickListener {
        void onItemClick(Release data);
    }

    /**
     * 按钮的点击监听
     */
    public interface ButtonClickListener {
        void onClick(int p, Release data);
    }
    // 将字符串转为时间戳
    public  long getTime(String stime) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date curDate =new  Date(System.currentTimeMillis());//获取当前时间
        String str = formatter.format(curDate);
        long st= ConvertUtils.timeDifference(stime,str);
        return st;
    }
}




